package com.pubinfo.passbook.common.model.dto.witeoff;

import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import com.pubinfo.passbook.common.entity.TPassbook;
import com.pubinfo.passbook.common.entity.TWiteoffPayMent;
import com.pubinfo.passbook.common.model.bo.witeoff.InvoiceInfo;
import com.pubinfo.passbook.common.model.bo.witeoff.MedicineOnJobOrRetireBO;
import com.pubinfo.passbook.common.model.bo.witeoff.WiteoffBase;
import com.pubinfo.passbook.common.model.bo.witeoff.subitem.MedicineOnJobOrRetireSubitem;
import com.pubinfo.passbook.common.model.ruler.UserAcc;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 在职医药/退休医药数据,接收前端报账单数据
 */
@Data
@NoArgsConstructor
@ApiModel(value = "MedicineOnJobOrRetireDTO", description = "在职医药/退休医药数据,接收前端报账单数据")
@JacksonXmlRootElement(localName = "item")
public class MedicineOnJobOrRetireDTO extends WiteoffBase implements Serializable {
  private static final long serialVersionUID = -3520339785214702403L;
  
  @ApiModelProperty(value = "是否为特殊病种", hidden = true)
  public Boolean isSpecial=false;
  @ApiModelProperty(value = "是否为特殊人群", hidden = true)
  public Boolean isSpecialUser;
  @ApiModelProperty(value = "报销类型，0：门诊，1住院", required = true)
  public Byte reimbursementType;
  @ApiModelProperty(value = "当前用户账号")
  public String acctId;
  @ApiModelProperty(hidden = true)
  public String detalis;
  @ApiModelProperty(value = "只有在职员工的供属才有权限报销,是否为供属报销")
  public Boolean isGenus;
  @ApiModelProperty(value = "报销人身份证号,如果isGenus为true,则此项显示,并且弹出框列表选择供属表中的人员")
  public String acctCertNo;
  @ApiModelProperty(value = "住院合计金额")
  public BigDecimal hospitalizationTotalAmount = new BigDecimal(0);
  @ApiModelProperty(value = "门诊合计金额")
  public BigDecimal outpatientDepartmentTotalAmount = new BigDecimal(0);
  @ApiModelProperty(value = "住院所能报销的金额")
  public BigDecimal hospitalizationSum = new BigDecimal(0);
  @ApiModelProperty(value = "门诊所能报销的金额")
  public BigDecimal outpatientDepartmentSum=new BigDecimal(0);
  @ApiModelProperty(value = "住院票据数量")
  public Integer hospitalizationNub=0;
  @ApiModelProperty(value = "门诊票据数量")
  public Integer outpatientDepartmentNub=0;
  @ApiModelProperty(value = "附件id集合")
  public List<String> TFileEnclosureFtpIds;

  public MedicineOnJobOrRetireDTO(MedicineOnJobOrRetireBO bo) {
    BeanUtils.copyProperties(bo, this);
  }
  
  public MedicineOnJobOrRetireDTO(MedicineOnJobOrRetireDTO bo) {
    BeanUtils.copyProperties(bo, this);
  }
  
  public MedicineOnJobOrRetireDTO(MedicineOnJobOrRetireDTO dto, List<TPassbook> genusOpcItem) {
    BeanUtils.copyProperties(dto, this);
    this.invoiceIds = genusOpcItem.stream().map(TPassbook::getEInvoiceId).collect(Collectors.toList());
  }
  
  //
//  @JacksonXmlElementWrapper(localName = "lineItems")
//  @JacksonXmlProperty(localName = "lineItem")
//  public List<MedicineOnJobOrRetireSubitem> getLineItems() {
//    return lineItems;
//  }
//
//  @JacksonXmlElementWrapper(localName = "invoiceIds")
//  @JacksonXmlProperty(localName = "invoiceId")
//  public List<String> getInvoiceIds() {
//    return invoiceIds;
//  }
  public void setUserAcc(UserAcc userAcc) {
    this.desc = "报" + userAcc.getName() + "医药费";
    this.account = userAcc.getAccount();
    this.fillInName = userAcc.getName();
    this.fillInOrgCode = userAcc.getOrgCode();
  }
  
  /**
   * @param invoiceInfos  报账票据信息
   * @param type          类型 如果type为0 则为在职员工,为1,则为离退休员工
   * @param sapCompayCode 公司代码
   * @param userAcc       报账人信息
   * @param staffCode     人员账号 无@ZJ
   */
  public MedicineOnJobOrRetireDTO(String profile, Integer type, String sapCompayCode, InvoiceInfo invoiceInfo, UserAcc userAcc, Boolean isGenus, String staffCode, String budgetSet, BigDecimal sum,String desc) {
    //需要问的sapCompayCode
    //如果type为0 则为环保费,为1,则为社会团体费
    //默认金额小于50000,如果大于50000走人工
    // if (type == 0) {
    if(StringUtils.isEmpty(desc))
    {
      this.desc = "报" + userAcc.getName() + "医药费";
    }
    else
    {
      this.desc=desc;
    }
//    } else if (type == 1) {
//      this.desc = "报"+userAcc.getName()+"医药费";
//    }
    this.account = userAcc.getAccount();
    this.fillInName = userAcc.getName();
    this.fillInOrgCode = userAcc.getOrgCode();
    // account  fillInName  fillInOrgCode sapCompayCode
    if (invoiceInfo != null) {
      List<String> invoiceIds = new ArrayList<>();
      invoiceIds.add(invoiceInfo.getId());
    }
    this.sum = sum;
  
    this.invoiceIds = invoiceIds;
    if (type == 1) {
      this.economicItemCode = "10040808";
      this.economicItemName = "医药";
      this.isStaffPayment = 1;
    } else {
      this.economicItemCode = "10040701";
      this.economicItemName = "离退休人员相关费用";
      this.isStaffPayment = 0;
    }
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    this.happenDate = dateFormat.format(new Date());
    String budgetSetStr = new SimpleDateFormat("yyyy-MM").format(new Date());
    if (StringUtils.isEmpty(budgetSet)) {
      this.budgetSet = budgetSetStr;
    } else {
    
      this.budgetSet = budgetSet;
    }
    this.accountCode = "1002B88P00";
    this.accountName = "省公司财务公司综合户01001";
    this.sapCompayCode = sapCompayCode;
//    if (!CollectionUtils.isEmpty(invoiceInfos)) {
//      for (TWiteoffInvoiceInfo info : invoiceInfos) {
//        sum = sum.add(new BigDecimal(info.getSum()));
//      }
//    }
    this.invoiceType = 19; //12-电子发票-普票
    //delegationBankCode  委托划扣银行账号不是必填，这个一般是收支方式为已支付的才要填
    //供应商不用
    //relateSuppliers ? payMentItems
    //this.invoiceInfos = invoiceInfos;
    MedicineOnJobOrRetireSubitem subitem = new MedicineOnJobOrRetireSubitem(profile, sum, type, isGenus, sapCompayCode, userAcc);
    this.lineItems.add(subitem);
  
    this.isSpecial = false;

    // 自动报账不传供应商
//    List<TWiteoffRelateSupplier> relateSuppliers = new ArrayList<>();
//    TWiteoffRelateSupplier relateSupplier = new TWiteoffRelateSupplier(supplier, sum.doubleValue());
//    relateSuppliers.add(relateSupplier);
    // this.relateSuppliers = relateSuppliers;
  
    //收款方
    if (!StringUtils.isEmpty(staffCode)) {
      TWiteoffPayMent bank = new TWiteoffPayMent(staffCode);
      this.payMentItems.add(bank);
    }
  
  }
}
